package com.ruoyi.web.controller.gcgl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DecimalCalculateUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gcgl.domain.GcGzt;
import com.ruoyi.gcgl.mapper.GcSendTipMapper;
import com.ruoyi.gcgl.service.IGcGztService;
import com.ruoyi.gcgl.service.IGcSendTipService;
import com.ruoyi.web.controller.tool.AliyunSmsUtils;

/**
 * 工资条Controller
 * 
 * @author ruoyi
 * @date 2022-12-25
 */
@Controller
@RequestMapping("/gcgl/gzt")
public class GcGztController extends BaseController
{
    private String prefix = "gcgl/gzt";

    @Autowired
    private IGcGztService gcGztService;
    @Autowired
    private IGcSendTipService gcSendTipService;
    @Autowired
    private GcSendTipMapper gcSendTipMapper;

    @RequiresPermissions("gcgl:gzt:view")
    @GetMapping()
    public String gzt()
    {
        return prefix + "/gzt";
    }
    
    /**
     * 查询工资条列表
     */
    @PostMapping("/list4")
    @ResponseBody
    public TableDataInfo list4(GcGzt gcGzt)
    {
        startPage();
        List<GcGzt> list = gcGztService.selectGcGzt4List(gcGzt);
        return getDataTable(list);
    }
    
    @GetMapping("/gztItem/{years}")
    public String gztItem(@PathVariable("years") String years,ModelMap mmap){
    	mmap.put("years",years);
        return prefix + "/gztItem";
    }

    /**
     * 查询工资条列表
     */
    @PostMapping("/listItem")
    @ResponseBody
    public TableDataInfo listItem(GcGzt gcGzt)
    {
        startPage();
        gcGzt.setDeleteFlag(1);
        List<GcGzt> list = gcGztService.selectGcGztList(gcGzt);
        return getDataTable(list);
    }

    /**
     * 导出工资条列表
     */
    @RequiresPermissions("gcgl:gzt:export")
    @Log(title = "工资条", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GcGzt gcGzt)
    {
        List<GcGzt> list = gcGztService.selectGcGztList(gcGzt);
        ExcelUtil<GcGzt> util = new ExcelUtil<GcGzt>(GcGzt.class);
        return util.exportExcel(list, "工资条数据");
    }

    /**
     * 新增工资条
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
    	mmap.put("years",DateUtils.parseDateToStr("yyyy-MM", new Date()));
        return prefix + "/add";
    }

    /**
     * 新增保存工资条
     */
    @RequiresPermissions("gcgl:gzt:add")
    @Log(title = "工资条", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public synchronized AjaxResult addSave(GcGzt gcGzt)
    {
    	gcGzt.setDeleteFlag(1);
    	gcGzt.setStatus(0);
    	gcGzt.setCreateUser(getLoginName());
    	gcGzt.setCreateAt(new Date());
    	gcGzt.setUpdateUser(getLoginName());
    	gcGzt.setUpdateAt(new Date());
    	gcGztService.insertGcGzt(gcGzt);
    	
    	//发送消息通知
//    	gcSendTipService.sendTip("gzt", "工资条", gcGzt.getYears(), 
//    			gcGzt.getCreateUser(), DateUtils.dateTime(gcGzt.getCreateAt()), 
//    			gcGzt.getStatus().toString(), null, gcGzt.getId().toString());
    	
    	try {
//    		List<Map> userlis = gcSendTipMapper.selectGcToUserByName(gcGzt.getCreateUser());
//    		if(userlis!=null && userlis.size()>0){
//    			String phonenumber = userlis.get(0).get("phonenumber").toString();//创建人
    		String phonenumber = caiw;
    			AliyunSmsUtils.returnSmsTz(phonenumber);
//    		}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
        return toAjax(1);
    }

    /**
     * 修改工资条
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        GcGzt gcGzt = gcGztService.selectGcGztById(id);
        mmap.put("gcGzt", gcGzt);
        return prefix + "/edit";
    }

    /**
     * 修改保存工资条
     */
    @Log(title = "工资条", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GcGzt gcGzt)
    {
    	gcGzt.setStatus(0);
    	gcGzt.setUpdateUser(getLoginName());
    	gcGzt.setUpdateAt(new Date());
        return toAjax(gcGztService.updateGcGzt(gcGzt));
    }

    /**
     * 删除工资条
     */
    @RequiresPermissions("gcgl:gzt:remove")
    @Log(title = "工资条", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(gcGztService.deleteGcGztByIds(ids));
    }
    
    //财务审批
    @RequiresPermissions("gcgl:gzt:cwpsview")
    @GetMapping("/cwps")
    public String cwps()
    {
        return prefix + "/gztcwps";
    }
    
    @PostMapping("/cwpslist")
    @ResponseBody
    public TableDataInfo cwpslist(GcGzt gcGzt)
    {
        startPage();
        gcGzt.setDeleteFlag(1);
        gcGzt.setStatus(0);
        List<GcGzt> list = gcGztService.selectGcGzt4List(gcGzt);
        return getDataTable(list);
    }
    
    @Log(title = "通过", businessType = BusinessType.UPDATE)
    @PostMapping("/cwyes")
    @ResponseBody
    public AjaxResult cwyes(String years, ModelMap mmap){
		GcGzt obj = new GcGzt();
		obj.setYears(years);
		obj.setStatus(1);
		obj.setCwsh(getLoginName());
		obj.setCwshAt(new Date());
		gcGztService.updateGcGzt4(obj);
		
		//发送消息通知
//    	gcSendTipService.sendTip("gzt", "工资条", years, 
//    			obj.getCwsh(), DateUtils.dateTime(obj.getCwshAt()), 
//    			obj.getStatus().toString(), null, years);
		
		try {
//    		List<Map> userlis = gcSendTipMapper.selectGcToUserByName(obj.getCwsh());
//    		if(userlis!=null && userlis.size()>0){
//    			String phonenumber = userlis.get(0).get("phonenumber").toString();//创建人
			String phonenumber = zjl;
    			AliyunSmsUtils.returnSmsTz(phonenumber);
//    		}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
    	
    	return toAjax(1);
    }
    
    @Log(title = "不通过", businessType = BusinessType.UPDATE)
    @PostMapping("/cwno")
    @ResponseBody
    public AjaxResult cwno(String years, ModelMap mmap){
		GcGzt obj = new GcGzt();
		obj.setYears(years);
		obj.setStatus(2);
		obj.setCwsh(getLoginName());
		obj.setCwshAt(new Date());
		gcGztService.updateGcGzt4(obj);
		
		//发送消息通知
//    	gcSendTipService.sendTip("gzt", "工资条", years, 
//    			obj.getCwsh(), DateUtils.dateTime(obj.getCwshAt()), 
//    			obj.getStatus().toString(), null, years);
    	
    	try {
    		List<Map> userlis = gcSendTipMapper.selectGcToUserByName(obj.getCwsh());
    		if(userlis!=null && userlis.size()>0){
    			String phonenumber = userlis.get(0).get("phonenumber").toString();//创建人
    			AliyunSmsUtils.returnSmsTz(phonenumber);
    		}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
    	return toAjax(1);
    }
    
    /////总经理审批
    @RequiresPermissions("gcgl:gzt:zjlpsview")
    @GetMapping("/zjlps")
    public String zjlps()
    {
        return prefix + "/gztzjlps";
    }
    
    @PostMapping("/zjlpslist")
    @ResponseBody
    public TableDataInfo zjlpslist(GcGzt gcGzt)
    {
        startPage();
        gcGzt.setDeleteFlag(1);
        gcGzt.setStatus(1);
        List<GcGzt> list = gcGztService.selectGcGzt4List(gcGzt);
        return getDataTable(list);
    }
    
    @Log(title = "通过", businessType = BusinessType.UPDATE)
    @PostMapping("/zjlyes")
    @ResponseBody
    public AjaxResult zjlyes(String years, ModelMap mmap){
		GcGzt obj = new GcGzt();
		obj.setYears(years);
		obj.setStatus(3);
		obj.setZjlsb(getLoginName());
		obj.setZjlsbAt(new Date());
		gcGztService.updateGcGzt4(obj);
		
		//发送消息通知
//    	gcSendTipService.sendTip("gzt", "工资条", years, 
//    			obj.getCwsh(), DateUtils.dateTime(obj.getCwshAt()), 
//    			obj.getStatus().toString(), null, years);
    	
    	try {
    		List<Map> userlis = gcSendTipMapper.selectGcToUserByName(obj.getCwsh());
    		if(userlis!=null && userlis.size()>0){
    			String phonenumber = userlis.get(0).get("phonenumber").toString();//创建人
    			AliyunSmsUtils.returnSmsTz(phonenumber);
    		}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
    	
    	return toAjax(1);
    }
    
    @Log(title = "不通过", businessType = BusinessType.UPDATE)
    @PostMapping("/zjlno")
    @ResponseBody
    public AjaxResult zjlno(String years, ModelMap mmap){
		GcGzt obj = new GcGzt();
		obj.setYears(years);
		obj.setStatus(4);
		obj.setZjlsb(getLoginName());
		obj.setZjlsbAt(new Date());
		gcGztService.updateGcGzt4(obj);
		
		//发送消息通知
//    	gcSendTipService.sendTip("gzt", "工资条", years, 
//    			obj.getCwsh(), DateUtils.dateTime(obj.getCwshAt()), 
//    			obj.getStatus().toString(), null, years);
		
		try {
    		List<Map> userlis = gcSendTipMapper.selectGcToUserByName(obj.getCwsh());
    		if(userlis!=null && userlis.size()>0){
    			String phonenumber = userlis.get(0).get("phonenumber").toString();//创建人
    			AliyunSmsUtils.returnSmsTz(phonenumber);
    		}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
    	
    	return toAjax(1);
    }
    
    @GetMapping("/gztprint/{years}")
    public String gztprint(@PathVariable("years") String years, ModelMap mmap){
    	GcGzt gcGzt = new GcGzt();
    	gcGzt.setDeleteFlag(1);
    	gcGzt.setYears(years);
    	List<GcGzt> lis = gcGztService.selectGcGztList(gcGzt);
    	returnGzt(lis);
        mmap.put("lis", lis);
        mmap.put("years", lis.get(0).getYears());
        mmap.put("cwsh", lis.get(0).getCwsh());
        mmap.put("zjlsb", lis.get(0).getZjlsb());
        return prefix + "/gztprint";
    }
    
    private List<GcGzt> returnGzt(List<GcGzt> lis){
    	double jbgz = 0.0;
    	double gwjt = 0.0;
    	double wcbt = 0.0;
    	double txbt = 0.0;
    	double jtbt = 0.0;
    	double yfgz = 0.0;
    	double qt = 0.0;
    	double sbgrcd = 0.0;
    	double sbgscd = 0.0;
    	double gjjgrcd = 0.0;
    	double gjjgscd = 0.0;
    	double grsds = 0.0;
    	double hj = 0.0;
    	double sfgz = 0.0;
    	
    	for(GcGzt obj:lis){
    		jbgz=DecimalCalculateUtil.add(jbgz, obj.getJbgz());
    		gwjt=DecimalCalculateUtil.add(gwjt, obj.getGwjt());
    		wcbt=DecimalCalculateUtil.add(wcbt, obj.getWcbt());
    		txbt=DecimalCalculateUtil.add(txbt, obj.getTxbt());
    		jtbt=DecimalCalculateUtil.add(jtbt, obj.getJtbt());
    		yfgz=DecimalCalculateUtil.add(yfgz, obj.getYfgz());
    		qt=DecimalCalculateUtil.add(qt, obj.getQt());
    		sbgrcd=DecimalCalculateUtil.add(sbgrcd, obj.getSbgrcd());
    		sbgscd=DecimalCalculateUtil.add(sbgscd, obj.getSbgscd());
    		gjjgrcd=DecimalCalculateUtil.add(gjjgrcd, obj.getGjjgrcd());
    		gjjgscd=DecimalCalculateUtil.add(gjjgscd, obj.getGjjgscd());
    		grsds=DecimalCalculateUtil.add(grsds, obj.getGrsds());
    		hj=DecimalCalculateUtil.add(hj, obj.getHj());
    		sfgz=DecimalCalculateUtil.add(sfgz, obj.getSfgz());
    	}
    	
    	GcGzt l_obj = new GcGzt();
		l_obj.setJbgz(jbgz);
		l_obj.setGwjt(gwjt);
		l_obj.setWcbt(wcbt);
		l_obj.setTxbt(txbt);
		l_obj.setJtbt(jtbt);
		l_obj.setYfgz(yfgz);
		l_obj.setQt(qt);
		l_obj.setSbgrcd(sbgrcd);
		l_obj.setSbgscd(sbgscd);
		l_obj.setGjjgrcd(gjjgrcd);
		l_obj.setGjjgscd(gjjgscd);
		l_obj.setGrsds(grsds);
		l_obj.setHj(hj);
		l_obj.setSfgz(sfgz);
		lis.add(l_obj);
		return lis;
    }
    
    @GetMapping("/gztsp/{years}")
    public String gztsp(@PathVariable("years") String years, ModelMap mmap){
    	GcGzt gcGzt = new GcGzt();
    	gcGzt.setDeleteFlag(1);
    	gcGzt.setYears(years);
    	List<GcGzt> lis = gcGztService.selectGcGztList(gcGzt);
    	returnGzt(lis);
        mmap.put("lis", lis);
        mmap.put("years", lis.get(0).getYears());
        mmap.put("cwsh", lis.get(0).getCwsh());
        mmap.put("zjlsb", lis.get(0).getZjlsb());
        return prefix + "/gztsp";
    }
    
    /**
     * 修改工资条
     */
    @GetMapping("/gztCopy/{id}")
    public String gztCopy(@PathVariable("id") Long id, ModelMap mmap){
        GcGzt gcGzt = gcGztService.selectGcGztById(id);
        mmap.put("gcGzt", gcGzt);
        return prefix + "/gztCopy";
    }
    
    @Log(title = "复制", businessType = BusinessType.INSERT)
    @PostMapping("/copy")
    @ResponseBody
    public AjaxResult copy(String years,ModelMap mmap){
    	GcGzt gcGzt = new GcGzt();
    	gcGzt.setYears(years);
    	gcGzt.setDeleteFlag(1);
    	List<GcGzt> gcGztlis = gcGztService.selectGcGztList(gcGzt);
    	String copyYears = DateUtils.month(DateUtils.parseDateToStr("yyyy-MM", DateUtils.getNowDate()));
    	if(gcGztlis!=null && gcGztlis.size()>0){
    		for(GcGzt obj:gcGztlis){
    			obj.setId(null);
    			obj.setStatus(0);
    			obj.setYears(copyYears);
    			obj.setDeleteFlag(1);
    			obj.setCreateAt(new Date());
    			obj.setCreateUser(getLoginName());
    			obj.setUpdateAt(new Date());
    			obj.setUpdateUser(getLoginName());
    			obj.setCwshAt(null);
    			obj.setCwsh(null);
    			obj.setZjlsb(null);
    			obj.setZjlsbAt(null);
    			gcGztService.insertGcGzt(obj);
    		}
    	}
    	return toAjax(1);
    }
    
    @GetMapping("/gztcwsp/{years}")
    public String gztcwsp(@PathVariable("years") String years, ModelMap mmap){
    	GcGzt gcGzt = new GcGzt();
    	gcGzt.setDeleteFlag(1);
    	gcGzt.setYears(years);
    	List<GcGzt> lis = gcGztService.selectGcGztList(gcGzt);
    	returnGzt(lis);
        mmap.put("lis", lis);
        mmap.put("years", lis.get(0).getYears());
        mmap.put("cwsh", lis.get(0).getCwsh());
        mmap.put("zjlsb", lis.get(0).getZjlsb());
        return prefix + "/gztcwsp";
    }
    
    @GetMapping("/gztzjlsp/{years}")
    public String gztzjlsp(@PathVariable("years") String years, ModelMap mmap){
    	GcGzt gcGzt = new GcGzt();
    	gcGzt.setDeleteFlag(1);
    	gcGzt.setYears(years);
    	List<GcGzt> lis = gcGztService.selectGcGztList(gcGzt);
    	returnGzt(lis);    	
        mmap.put("lis", lis);
        mmap.put("years", lis.get(0).getYears());
        mmap.put("cwsh", lis.get(0).getCwsh());
        mmap.put("zjlsb", lis.get(0).getZjlsb());
        return prefix + "/gztzjlsp";
    }
}